<?php

$thisPage = "personalprofile";
$subpage = getRequestParameter("subpage", false);

TemplateHelper::useLayout("form");

$params = unserialize(stripslashes(getrequestparameter("params")));

addDataservice("submit");

function buildLayout(&$smarty) {
    global $subpage;
    if ($subpage == "") {
        $smarty->assign("form", getProfileFormData($smarty));
    } else if ($subpage == "success") {
        $smarty->assign("form", getSuccessData($smarty));
    } else if ($subpage == "success_delete") {
        $smarty->assign("form", getSuccessDeleteData($smarty));
    }
}

function executeService($serviceName, $params, $smarty) {
    switch ($serviceName) {
        case "submit" : { return service_submit($params); }
    }
}

function service_submit($params) {
    $success = true;
    $errorMsg = "";
    if ($params['delete'] == "0") {
        $captcha = getCaptchaObject();
        if (!$captcha->validate($params['random'])) {
            $errorMsg = "Captcha session key does not exist.";
            $success = false;
        } else if (!$captcha->verify($params['captcha'])) {
            $errorMsg = "You have entered a wrong captcha code.";
            $success = false;
        }
    }
    if ($success) {
        Database::loadModel("user");
        if ($params['delete'] == "0") {
            $userdata = array();
            if ($params['password'] != "") {
                if ($params['password'] == $params['password2']) {
                    $userdata['password'] = $params['password'];
                }
            }
            $userdata['firstname'] = $params['firstname'];
            $userdata['lastname'] = $params['lastname'];
            $userdata['title'] = $params['title'];
            $userdata['user_id'] = Auth::getUserProperty("user_id");
            Database::update($userdata, true);
            $result = serviceSuccessfulWithRedirect(array(), "personalprofile", "success");
        } else {
            if (Database::getModel()->checkUserLogin(Auth::getUserProperty("username"), $params['removepassword'])) {
                Database::remove(Auth::getUserproperty("user_id"));
                if (Auth::getUserProperty("company_profile_id")) {
                    Database::loadModel("companies");
                    Database::remove(Auth::getUserProperty("company_profile_id"));
                }
                $result = serviceSuccessfulWithRedirect(array(), "personalprofile", "success_delete");
            } else {
                $result = serviceFailure(array(), "Your password is incorrect");
            }
        }
    }
    if ($success) {
    } else {
        $result = serviceFailure(array(), $errorMsg);
    }
    return $result;
}

function getPageHeader() {
    return TemplateHelper::getDefaultPageHeader();
}

function getPageFooter() {
    return TemplateHelper::getDefaultPageFooter();
}

function getProfileFormData(&$smarty) {
    global $thisPage;
    initDatabase();
    Database::loadModel("user");
    $userdata = Database::getItem(Auth::getUserProperty("user_id"));
    $smarty->assign("captcha", TemplateHelper::addCaptchaService());
    $smarty->assign("profile", $userdata);
    return array(
        title => "Password change",
        content => $smarty->fetch(TemplateHelper::getTemplateFile($thisPage, "personalprofile_content"))
    );
}

function getSuccessData(&$smarty) {
    global $thisPage;
    return array(
        title => "Password change",
        content => $smarty->fetch(TemplateHelper::getTemplateFile($thisPage, "personalprofile_success"))
    );
}

function getSuccessDeleteData(&$smarty) {
    global $thisPage;
    return array(
        title => "Password change",
        content => $smarty->fetch(TemplateHelper::getTemplateFile($thisPage, "personalprofile_success_delete"))
    );
}

?>
